package cn.tedu.cqhotel.controller;

import cn.tedu.cqhotel.pojo.dto.RoomAddNewDTO;
import cn.tedu.cqhotel.pojo.dto.RoomUpdateDTO;
import cn.tedu.cqhotel.pojo.vo.RoomListItemVO;
import cn.tedu.cqhotel.pojo.vo.RoomStandardVO;
import cn.tedu.cqhotel.service.IRoomService;
import cn.tedu.cqhotel.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 处理房间相关请求的控制器
 *
 * @author zhouyucheng
 */
@Slf4j
@RestController
@RequestMapping("/rooms")
@Api(tags = "04.房间管理模块")
public class RoomController {

    @Autowired
    private IRoomService roomService;


    @ApiOperation("添加房间")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(RoomAddNewDTO roomAddNewDTO) {
        log.debug("开始处理[添加房间信息]的请求,{}", roomAddNewDTO);
        roomService.addNew(roomAddNewDTO);
        return JsonResult.ok();
    }


    @ApiOperation("删除房间")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "roomId", value = "房间编号", required = true, dataType = "int")
    @PostMapping("/{roomId:[\\d]+}/delete")
    public JsonResult<Void> delete(@PathVariable Integer roomId) {
        log.debug("开始处理[删除房间]的请求.roomId:{}", roomId);
        roomService.deleteByRoomId(roomId);
        return JsonResult.ok();
    }

    @ApiOperation("根据房间id修改房间信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "roomId", value = "房间编号", required = true, dataType = "int")
    @PostMapping("/{roomId:[0-9]+}/update")
    public JsonResult<Void> updateByRoomId(@PathVariable Integer roomId, RoomUpdateDTO roomUpdateDTO) {
        log.debug("开始处理【根据房间id修改房间信息】的请求：roomId={}, roomUpdateDTO={}", roomId, roomUpdateDTO);
        roomService.updateByRoomId(roomId, roomUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据房间编号获取房间详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "roomId", value = "房间编号", required = true, dataType = "int")
    @PostMapping("/{roomId:[0-9]+}")
    public JsonResult<RoomStandardVO> getStandardByRoomId(@PathVariable Integer roomId) {
        log.debug("开始处理【根据房间编号获取房间详情】的请求：roomId={}", roomId);
        RoomStandardVO standardByRoomId = roomService.getStandardByRoomId(roomId);
        return JsonResult.ok(standardByRoomId);
    }


    @ApiOperation("查询房间列表")
    @ApiOperationSupport(order = 410)
    @GetMapping("")
    public JsonResult<List<RoomListItemVO>> list() {
        log.debug("开始处理【查询房间列表】的请求……");
        List<RoomListItemVO> list = roomService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("根据是否入住查询房间列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParam(name = "isUsed", value = "是否入住", required = true, dataType = "int")
    @GetMapping("/{isUsed:[01]}/list")
    public JsonResult<List<RoomListItemVO>> listByIsUsed(@PathVariable Integer isUsed) {
        log.debug("开始处理【根据是否入住查询房间列表】的请求,isUsed:{}", isUsed);
        List<RoomListItemVO> list = roomService.listByIsUsed(isUsed);
        return JsonResult.ok(list);
    }

    @ApiOperation("根据房间可住人数类型和房间类型查询房间信息列表")
    @ApiOperationSupport(order = 425)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "version", value = "可住人数类型", required = true, dataType = "int"),
            @ApiImplicitParam(name = "type", value = "房间类型", required = true, dataType = "int")
    })

    @GetMapping("/list/version-type}")
    public JsonResult<List<RoomListItemVO>> listByVersionAndType(Integer version, Integer type) {
        log.debug("开始处理【根据房间可住人数类型和房间类型查询房间信息列表】的请求,version:{},type:{}", version, type);
        List<RoomListItemVO> listByVersionAndType = roomService.listByVersionAndType(version, type);
        return JsonResult.ok(listByVersionAndType);
    }

    @ApiOperation("根据房间单价查询房间信息列表")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParam(name = "price", value = "房间单价", required = true, dataType = "BigDecimal")
    @GetMapping("/list/price")
    public JsonResult<List<RoomListItemVO>> listByIsUsed(BigDecimal price) {
        log.debug("开始处理【根据房间单价查询房间信息列表】的请求,price:{}", price);
        List<RoomListItemVO> listByPrice = roomService.listByPrice(price);
        return JsonResult.ok(listByPrice);
    }


}
